/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.cef.generator.coreGenerator.base.accessorgen.method;


//using MethodAccessModifier = Inspur.Gsp.Caf.Generator.BaseInfoItem.MethodAccessModifier;

import com.inspur.edp.caf.generator.baseInfo.ParameterInfo;
import com.inspur.edp.caf.generator.baseInfo.TypeInfo;
import com.inspur.edp.caf.generator.method.ClassMethodGenerator;
import com.inspur.edp.cef.api.attr.ChildAttribute;
import com.inspur.edp.cef.designtime.api.IGspCommonDataType;
import com.inspur.edp.cef.entity.entity.IEntityDataCollection;
import com.inspur.edp.cef.gencommon.CefPropertyInfo;
import com.inspur.edp.cef.gencommon.ReflectionUtils;
import com.inspur.edp.das.commonmodel.IGspCommonObject;
import java.util.List;
import org.eclipse.jdt.core.dom.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import org.eclipse.jdt.core.dom.Modifier.ModifierKeyword;

public class GetChildsGenerator extends ClassMethodGenerator
{

	private IGspCommonDataType dataType;
	private List<Modifier.ModifierKeyword> access;

	public GetChildsGenerator(IGspCommonDataType dataType, List<ModifierKeyword> access)
	{
		this.dataType = dataType;
		this.access = access;
	}


		///#region

	@Override
	protected boolean getIsOverride() {
		return true;
	}

	@Override
	protected String getMethodName()
	{
		return "innerGetChilds";
	}
	private static String VarChilds="childs";

	@Override
	protected TypeInfo getReturnType() {
		TypeInfo typeInfo=new TypeInfo(HashMap.class.getTypeName());
		addImport(HashMap.class.getTypeName());
		typeInfo.setIsParameterizedType(true);
		typeInfo.getArguments().add(new TypeInfo(String.class));
		typeInfo.getArguments().add(new TypeInfo(IEntityDataCollection.class));
		return typeInfo;
	}

	@Override
	protected ArrayList<Modifier.ModifierKeyword> getAccessModifiers() {
		return new ArrayList(access);
	}

	@Override
	protected ArrayList<ParameterInfo> getParameterCollection() {
		return null;
	}

	/*
	* Map<String,IEntityDataCollection> childs= new HashMap<>();
		childs.put("SalesOrderItem",getSalesOrderItems());
		return childs;
		* */
	@Override
	protected Block buildMethodBody() {
		Block block=ast.newBlock();
		addNewChildsStatement(block);
		addNewPubChildsStatement(block);
		addReturnChildsStatement(block);
		return  block;
	}

	//Map<String,IEntityDataCollection> childs= new HashMap<>();
	private void addNewChildsStatement(Block block) {
		VariableDeclarationFragment variableDeclarationFragment=ast.newVariableDeclarationFragment();
		variableDeclarationFragment.setName(ast.newSimpleName(VarChilds));

		ClassInstanceCreation classInstanceCreation=ast.newClassInstanceCreation();
		classInstanceCreation.setType(ast.newParameterizedType(new com.inspur.edp.caf.generator.baseInfo.TypeInfo(HashMap.class).getType(ast)));
		variableDeclarationFragment.setInitializer(classInstanceCreation);

		VariableDeclarationStatement variableDeclarationStatement=ast.newVariableDeclarationStatement(variableDeclarationFragment);
		addNewChildsType(variableDeclarationStatement);
		block.statements().add(variableDeclarationStatement);
	}

	//Map<String,IEntityDataCollection>
	private void addNewChildsType(VariableDeclarationStatement variableDeclarationStatement) {
		ParameterizedType parameterizedType=ast.newParameterizedType(new com.inspur.edp.caf.generator.baseInfo.TypeInfo(HashMap.class).getType(ast));
		parameterizedType.typeArguments().add(new com.inspur.edp.caf.generator.baseInfo.TypeInfo(String.class).getType(ast));
		parameterizedType.typeArguments().add(new com.inspur.edp.caf.generator.baseInfo.TypeInfo(IEntityDataCollection.class).getType(ast));
		addImport(IEntityDataCollection.class.getTypeName());
		variableDeclarationStatement.setType(parameterizedType);
	}

//	childs.put("SalesOrderItem",getSalesOrderItems());
	private void addNewPubChildsStatement(Block block)
	{
		if(dataType instanceof  IGspCommonObject ==false)
			return;
		for (IGspCommonObject childObject:((IGspCommonObject)dataType).getContainChildObjects())
		{
			addPutItemChildStatement(block,childObject);
		}
	}

	//	childs.put("SalesOrderItem",getSalesOrderItems());
	private void addPutItemChildStatement(Block block, IGspCommonObject childObject) {
		MethodInvocation methodInvocationPut=ast.newMethodInvocation();
		methodInvocationPut.setName(ast.newSimpleName("put"));
		methodInvocationPut.setExpression(ast.newSimpleName(VarChilds));

		StringLiteral stringLiteral=ast.newStringLiteral();
		stringLiteral.setLiteralValue(childObject.getCode());
		methodInvocationPut.arguments().add(stringLiteral);

		MethodInvocation methodInvocation1=ast.newMethodInvocation();
		methodInvocation1.setName(ast.newSimpleName("get"+childObject.getCode()+"s"));
		methodInvocationPut.arguments().add(methodInvocation1);

		block.statements().add(ast.newExpressionStatement(methodInvocationPut));
	}

//	return childs;
	private void addReturnChildsStatement(Block block)
	{
		ReturnStatement returnStatement=ast.newReturnStatement();
		returnStatement.setExpression(ast.newSimpleName(VarChilds));
		block.statements().add(returnStatement);
	}
}
